// StudentViewController.swift
import UIKit

class StudentViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 初始化数据库
        let sqlManager = SQLManager.shareInstance()
        sqlManager.createDataBaseTableIfNeeded()
        
        // 查询课程表
        let querySQL = "SELECT * FROM course;"
        if let courses = sqlManager.queryDataBase(querySQL: querySQL) {
            for course in courses {
                if let id = course["id"] as? String,
                   let courseName = course["course_name"] as? String,
                   let teacher = course["teacher"] as? String {
                    let courseObj = Course(id: id, courseName: courseName, teacher: teacher)
                    print("课程 ID: \(courseObj.id), 课程名称: \(courseObj.courseName), 教师: \(courseObj.teacher)")
                }
            }
        }
    }
    
    // 选课方法
    func selectCourse(courseId: String) {
        let sqlManager = SQLManager.shareInstance()
        let selectSQL = "INSERT INTO selected_courses (student_name, course_id) VALUES ('student_name', '\(courseId)');"
        if sqlManager.execSQL(SQL: selectSQL) {
            print("选课成功！")
        } else {
            print("选课失败！")
        }
    }
    
    // 退课方法
    func dropCourse(courseId: String) {
        let sqlManager = SQLManager.shareInstance()
        let dropSQL = "DELETE FROM selected_courses WHERE student_name = 'student_name' AND course_id = '\(courseId)';"
        if sqlManager.execSQL(SQL: dropSQL) {
            print("退课成功！")
        } else {
            print("退课失败！")
        }
    }
}
